package com.gsn.poker.play;

import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.equations.Linear;
import aurelienribon.tweenengine.equations.Sine;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.gsn.desktop.engine.ActorUtility;
import com.gsn.engine.myplay.GsnGame;
import com.gsn.engine.tween.ActorAccessor;
import com.gsn.engine.tween.GsnTween;
import com.gsn.poker.asset.PokerSound;
import com.gsn.poker.asset.PokerTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class ChonQuanGroup extends Group implements ClickListener{
	public BoardLayer boardLayer;
	public float pad = 20;
	CardImg card1;
	CardImg card2;
	Image bgProgress;
	Image progress;
	public float updateScale = 0;
	public ChonQuanGroup(BoardLayer boardLayer, int id1, int id2) {
		transform = GsnGame.transform;
		this.boardLayer = boardLayer;
		card1 = new CardImg(id1);
		card2 = new CardImg(id2);
		bgProgress = new Image(PokerTexture.inst().timeMobai);
		progress = new Image(PokerTexture.inst().timeMobaiDown);
//		card1.width *= 1f;
//		card1.height *= 1f;
//		
//		card2.width *= 0.5f;
//		card2.height *= 0.5f;
		
		ActorUtility.setRatio(card2, -0.5f, 0f, card1.x + card1.width, 0);
		
		this.width = card2.x + card2.width;
		this.height = card2.height;
		
		width += 2 * pad;
		height += 3 * pad;
		
		addActor(card1);
		addActor(card2);
		addActor(bgProgress);
		addActor(progress);
		
		for (Actor actor : this.getActors()){			
			actor.x += pad;
			actor.y += pad / 3;
		}
		ActorUtility.setRatio(bgProgress, 0.5f, 0, this.width / 2, card1.y + card1.height * 1.1f);
		ActorUtility.setRatio(progress, 0.5f, 0, this.width / 2, card1.y + card1.height*1.1f);
		progress.scaleX = 0;
		Image bg = new Image(PokerTexture.inst().selectCardBG);
		bg.width = width;
		bg.height = height;
		addActorAt(0, bg);
		card1.setClickListener(this);
		card2.setClickListener(this);
		
		Image selectCard = new Image(PokerTexture.inst().selectCard);
		ActorUtility.setCenter(selectCard, card2.x + card2.width / 2, card2.y + card2.height / 2 );
		//selectCard.y = 0;
		addActor(selectCard);
		Tween.to(progress, ActorAccessor.SCALE_XY, 3f).target(1, 1).ease(Linear.INOUT).start(MyPoker.game.tweenManger);
	}
	@Override
	public void click(Actor actor, float x, float y) {
		PokerSound.playClick();
		if (actor == card1){
			MyPoker.client.send(PacketFactory.createShowFirstCard(0));
			boardLayer.showMyFirstCard(card1.getId());
		} else{						
			MyPoker.client.send(PacketFactory.createShowFirstCard(1));
			boardLayer.showMyFirstCard(card2.getId());
		}
		this.remove();
	}
}
